Semantic CSS.md
ASCII text
title: Let's write more semantic CSS topics: ["web", "css", "html"] ---
You probably wrote something like this at least once in your life:
<div class="card card--rounded card--primary"> <div class="card__image-container"> <img src="image.jpg" alt="A nice image" class="card__image"> <span class="card__image-caption">A nice image</span> </div> <div class="card__content"> <div class="card__header"> <div class="card__title">Hello, world!</div> </div> <p class="card__text"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p> </div> <div class="card__footer"> <button class="btn btn--primary btn--raised btn--accent card__button card__button--primary">Click me!</button> <button class="btn btn--secondary btn--raised btn--accent card__button card__button--secondary">Click me!</button> </div> </div>
Or this:
<div class="max-w-sm rounded overflow-hidden shadow-lg"> <div> <img class="w-full" src="image.jpg" alt="A nice image"> <span class="text-gray-500 text-base">A nice image</span> </div> <div class="px-6 py-4"> <div> <div class="font-bold text-xl mb-2">Hello, world!</div> </div> <p class="text-gray-700 text-base"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p> </div> <div class="px-6 py-4"> <button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Click me!</button> <button class="bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded">Click me!</button> </div> </div>
The second one is an adapted example from the Tailwind CSS docs. The first one is a variant that uses BEM instead. Both of them have many problems.
HTML has got over 100 elements you could use to structure your content. These examples use only
5: div
, span
, p
, img
, and button
. This is not a problem in itself for small components,
but it can indicate one. Using div
and span
for everything means you're misusing HTML. This
is wrong: don't overlook HTML. JS or CSS may be more interesting, but the document language of
the WWW is HTML.
The first example uses classes in place of elements. This creates extra work for both the HTML and CSS author. The CSS still mirrors the HTML structure, and the HTML is much more verbose than it needs to be. The word "button" or "btn" appears 8 times for each button. Ideally, it should appear two times: once in the opening tag and once in the closing tag.
The second example intentionally has the same markup tree as the first one. However, the classes
changed a lot. Tailwind uses classes instead of CSS rules. It leads to repetition. If you don't
want to repeat, you use components. But what if you don't do components? Then use @apply
in
CSS. Yes, CSS. So you're basically writing CSS only with a different syntax and less flexibility.
A Simpler Way
Let's strip the classes and focus on the markup tree for now. The two examples are identical in this regard.
<div> <div> <img src="image.jpg" alt="A nice image"> <span>A nice image</span> </div> <div> <div> <div>Hello, world!</div> </div> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p> </div> <div> <button>Click me!</button> <button>Click me!</button> </div> </div>
Now you see what I said? This tree is not semantic at all. Let's find the appropriate elements for each generic one.
<article> <figure> <img src="image.jpg" alt="A nice image"> <figcaption>A nice image</figcaption> </figure> <section> <header> <h2>Hello, world!</h2> </header> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p> </section> <menu> <button>Click me!</button> <button>Click me!</button> </menu> </article>
In case you're not familiar with the new elements, the quick meaning is:
article - a self-contained piece of content that makes sense independently from the rest of the page
figure - a piece of content that is referenced from the main content, but can stand alone
figcaption - a caption for a
figure
's other content (optional)section - a thematic grouping of content, typically with a heading
header - header for the document or a smaller part of it, can include context, navigation or information about the content
h2 - a second-level section heading (you probably knew this one already)
menu - a list of commands available to take on a specific part of the content
Please read the MDN articles I linked if you want to know more about these elements.
Depending on the other needs of your website or application, you will probably need to add a few classes. However, unlike the other examples, classes should be used as little as possible. Let's remember some things from the examples:
The article is supposed to be a card and styled as such.
The first button is the primary action, and the second one is the secondary action.
In this site, let's say not all articles are cards, but since this one is a card, we'll classify it as such. Let's also say that the secondary buttons are more common, this means we'll add a class to the primary button and style that later.
<article class="card"> <figure> <img src="image.jpg" alt="A nice image"> <figcaption>A nice image</figcaption> </figure> <section> <header> <h2>Hello, world!</h2> </header> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p> </section> <menu> <button class="button-primary">Click me!</button> <button>Click me!</button> </menu> </article>
Now, let's write a basic stylesheet for this. It won't look exactly like the second example for
the sake of simplicity, but it could easily be made to look like that. We're going to use a CSS
selector you've probably only seen in resets and to set the font on the html
element, the
tag selector. We're also going to use some new CSS smarts to make the styles more maintainable.
html, button, input, select, textarea { font-family: system-ui, sans-serif; } article.card { background-color: whitesmoke; border-radius: 12px; box-shadow: 0 0 4px #00000040; display: flex; flex-direction: column; gap: 1rem; overflow: hidden; } figure { display: flex; flex-direction: column; gap: 0.25rem; } figure > img { width: 100%; height: auto; } figcaption { font-style: italic; opacity: 0.875; } article.card > section { padding-left: 1rem; padding-right: 1rem; } article.card > menu, menu.buttonbox { display: flex; gap: 1rem; justify-content: flex-end; } button, .button, /* provide alternative where it makes sense, since we may want to make something else look like a button */ input:is([type="button"], [type="submit"], [type="reset"]) { background-color: white; color: orange; border: 4px solid currentColor; padding: 0.5rem 1rem; display: inline-flex; align-items: center; gap: 0.5rem; border: none; border-radius: 4px; /* Border radii are a decoration so pixels are fine */ } :is(button, .button, input:is([type="button"], [type="submit"], [type="reset"])).button-primary { background-color: orange; color: white; }
Observations:
We provide alternatives for some tag selectors where it makes sense, in case we want to make something else look like a button. However, we don't force using both when it's already clear:
<button>
will produce a styled button, same as<a class="button">
.<button class="button">
is redundant.The
>
child selector is used to avoid leaking styles in more complex nested layouts.We use the
:is()
pseudo-class to group selectors that have the same styles. This is a new feature in CSS and it saves us from writing an enormous amount of combinations.
Conclusion
Now, writing HTML is much easier: the CSS will adapt to what you intended to describe. The CSS is also much easier to maintain: the style can be changed easily without changing the HTML. The elements are always styled automatically, and you can copy-paste a snippet of plain HTML and have it magically match the rest of your site.
A more complete framework for this could add some layout container utilities. For example, a
grid
class that makes the element a grid container and uses --width
and --gap
custom
properties to position the children. There could also be layout elements to use in place of
divs like x-hbox
and x-vbox
that are flex containers. This would indicate the default style,
and an additional class or ID would be used to make them responsive as well. Utility classes
aren't bad, but they should be used for the things that can't cause repetition - which side a
dialogue should emerge from, or whether to add padding in a generic row container.
Frameworks Using Semantic CSS
Pico CSS - does it very well, I should take some inspiration from it
Water.css - a very minimalistic CSS framework, primarily intended for publishing, but also includes interactive elements
MVP.css - a basic stylesheet for plain HTML made for any site to look acceptable
The roundabout also uses semantic CSS. Once the API is stabilised a little the CSS will be released as a framework.
You might not even need a framework.
1--- 2title: Let's write more semantic CSS 3topics: ["web", "css", "html"] 4--- 5 6You probably wrote something like this at least once in your life: 7```html 8<div class="card card--rounded card--primary"> 9<div class="card__image-container"> 10<img src="image.jpg" alt="A nice image" class="card__image"> 11<span class="card__image-caption">A nice image</span> 12</div> 13<div class="card__content"> 14<div class="card__header"> 15<div class="card__title">Hello, world!</div> 16</div> 17<p class="card__text"> 18Lorem ipsum dolor sit amet, consectetur adipiscing elit. 19</p> 20</div> 21<div class="card__footer"> 22<button class="btn btn--primary btn--raised btn--accent card__button card__button--primary">Click me!</button> 23<button class="btn btn--secondary btn--raised btn--accent card__button card__button--secondary">Click me!</button> 24</div> 25</div> 26``` 27Or this: 28```html 29<div class="max-w-sm rounded overflow-hidden shadow-lg"> 30<div> 31<img class="w-full" src="image.jpg" alt="A nice image"> 32<span class="text-gray-500 text-base">A nice image</span> 33</div> 34<div class="px-6 py-4"> 35<div> 36<div class="font-bold text-xl mb-2">Hello, world!</div> 37</div> 38<p class="text-gray-700 text-base"> 39Lorem ipsum dolor sit amet, consectetur adipiscing elit. 40</p> 41</div> 42<div class="px-6 py-4"> 43<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Click me!</button> 44<button class="bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded">Click me!</button> 45</div> 46</div> 47``` 48 49The second one is an adapted example from the **Tailwind** CSS docs. The first one is a variant that 50uses **BEM** instead. Both of them have *many* problems. 51 52HTML has got over 100 elements you could use to structure your content. These examples use only 535: `div`, `span`, `p`, `img`, and `button`. This is not a problem in itself for small components, 54but it can indicate one. Using `div` and `span` for everything means you're misusing HTML. This 55is wrong: don't overlook HTML. JS or CSS may be more interesting, but the document language of 56the WWW is HTML. 57 58The first example uses classes in place of elements. This creates extra work for both the HTML 59and CSS author. The CSS still mirrors the HTML structure, and the HTML is much more verbose than 60it needs to be. The word "button" or "btn" appears 8 times for each button. Ideally, it should 61appear two times: once in the opening tag and once in the closing tag. 62 63The second example intentionally has the same markup tree as the first one. However, the classes 64changed a lot. Tailwind uses classes instead of CSS rules. It leads to repetition. If you don't 65want to repeat, you use components. But what if you don't do components? Then use `@apply` in 66CSS. Yes, CSS. So you're basically writing CSS only with a different syntax and less flexibility. 67 68A Simpler Way 69------------- 70 71Let's strip the classes and focus on the markup tree for now. The two examples are identical in 72this regard. 73 74```html 75<div> 76<div> 77<img src="image.jpg" alt="A nice image"> 78<span>A nice image</span> 79</div> 80<div> 81<div> 82<div>Hello, world!</div> 83</div> 84<p> 85Lorem ipsum dolor sit amet, consectetur adipiscing elit. 86</p> 87</div> 88<div> 89<button>Click me!</button> 90<button>Click me!</button> 91</div> 92</div> 93``` 94 95Now you see what I said? This tree is not semantic at all. Let's find the appropriate elements for 96each generic one. 97 98```html 99<article> 100<figure> 101<img src="image.jpg" alt="A nice image"> 102<figcaption>A nice image</figcaption> 103</figure> 104<section> 105<header> 106<h2>Hello, world!</h2> 107</header> 108<p> 109Lorem ipsum dolor sit amet, consectetur adipiscing elit. 110</p> 111</section> 112<menu> 113<button>Click me!</button> 114<button>Click me!</button> 115</menu> 116</article> 117``` 118 119In case you're not familiar with the new elements, the quick meaning is: 120 121* [article](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/article) - a self-contained 122piece of content that makes sense independently from the rest of the page 123* [figure](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure) - a piece of content 124that is referenced from the main content, but can stand alone 125* [figcaption](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figcaption) - a caption 126for a `figure`'s other content (optional) 127* [section](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/section) - a thematic grouping 128of content, typically with a heading 129* [header](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/header) - header for the 130document or a smaller part of it, can include context, navigation or information about the 131content 132* [h2](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h2) - a second-level section 133heading (you probably knew this one already) 134* [menu](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/menu) - a list of commands 135available to take on a specific part of the content 136 137Please read the MDN articles I linked if you want to know more about these elements. 138 139Depending on the other needs of your website or application, you will probably need to add a few 140classes. However, unlike the other examples, classes should be used as little as possible. Let's 141remember some things from the examples: 142 143* The article is supposed to be a card and styled as such. 144* The first button is the primary action, and the second one is the secondary action. 145 146In this site, let's say not all articles are cards, but since this one *is* a card, we'll 147classify it as such. Let's also say that the secondary buttons are more common, this means we'll 148add a class to the primary button and style that later. 149 150```html 151<article class="card"> 152<figure> 153<img src="image.jpg" alt="A nice image"> 154<figcaption>A nice image</figcaption> 155</figure> 156<section> 157<header> 158<h2>Hello, world!</h2> 159</header> 160<p> 161Lorem ipsum dolor sit amet, consectetur adipiscing elit. 162</p> 163</section> 164<menu> 165<button class="button-primary">Click me!</button> 166<button>Click me!</button> 167</menu> 168</article> 169``` 170 171Now, let's write a basic stylesheet for this. It won't look exactly like the second example for 172the sake of simplicity, but it could easily be made to look like that. We're going to use a CSS 173selector you've probably only seen in resets and to set the font on the `html` element, the 174tag selector. We're also going to use some new CSS smarts to make the styles more maintainable. 175 176```css 177html, button, input, select, textarea { 178font-family: system-ui, sans-serif; 179} 180article.card { 181background-color: whitesmoke; 182border-radius: 12px; 183box-shadow: 0 0 4px #00000040; 184display: flex; 185flex-direction: column; 186gap: 1rem; 187overflow: hidden; 188} 189figure { 190display: flex; 191flex-direction: column; 192gap: 0.25rem; 193} 194figure > img { 195width: 100%; 196height: auto; 197} 198figcaption { 199font-style: italic; 200opacity: 0.875; 201} 202article.card > section { 203padding-left: 1rem; 204padding-right: 1rem; 205} 206article.card > menu, menu.buttonbox { 207display: flex; 208gap: 1rem; 209justify-content: flex-end; 210} 211button, .button, /* provide alternative where it makes sense, since we may want to make something else look like a button */ 212input:is([type="button"], [type="submit"], [type="reset"]) { 213background-color: white; 214color: orange; 215border: 4px solid currentColor; 216padding: 0.5rem 1rem; 217display: inline-flex; 218align-items: center; 219gap: 0.5rem; 220border: none; 221border-radius: 4px; /* Border radii are a decoration so pixels are fine */ 222} 223:is(button, .button, input:is([type="button"], [type="submit"], [type="reset"])).button-primary { 224background-color: orange; 225color: white; 226} 227``` 228 229Observations: 230* We provide alternatives for some tag selectors where it makes sense, in case we want to make 231something else look like a button. However, we don't force using both when it's already clear: 232`<button>` will produce a styled button, same as `<a class="button">`. `<button class="button">` 233is redundant. 234* The `>` child selector is used to avoid leaking styles in more complex nested layouts. 235* We use the `:is()` pseudo-class to group selectors that have the same styles. This is a new 236feature in CSS and it saves us from writing an enormous amount of combinations. 237 238Conclusion 239---------- 240 241Now, writing HTML is much easier: the CSS will adapt to what you intended to describe. The CSS 242is also much easier to maintain: the style can be changed easily without changing the HTML. The 243elements are always styled automatically, and you can copy-paste a snippet of plain HTML 244and have it magically match the rest of your site. 245 246A more complete framework for this could add some layout container utilities. For example, a 247`grid` class that makes the element a grid container and uses `--width` and `--gap` custom 248properties to position the children. There could also be layout *elements* to use in place of 249divs like `x-hbox` and `x-vbox` that are flex containers. This would indicate the default style, 250and an additional class or ID would be used to make them responsive as well. Utility classes 251aren't bad, but they should be used for the things that can't cause repetition - which side a 252dialogue should emerge from, or whether to add padding in a generic row container. 253 254Frameworks Using Semantic CSS 255------------------------------ 256 257* [Pico CSS](https://picocss.com/) - does it very well, I should take some inspiration from it 258* [Water.css](https://watercss.kognise.dev/) - a very minimalistic CSS framework, primarily intended 259for publishing, but also includes interactive elements 260* [MVP.css](https://andybrewer.github.io/mvp/) - a basic stylesheet for plain HTML made for any 261site to look acceptable 262* The roundabout also uses semantic CSS. Once the API is stabilised a little the CSS will be 263released as a framework. 264* You might not even need a framework. 265